<template>
  <div>
    <el-row>
      <el-col>
        <el-button
          style="margin-left: 20px"
          type="success"
          icon="el-icon-plus"
          size="medium"
          @click="add"
          v-if="$hasBP('bnt.menu.insert')"
          >添加目录</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-if="$hasBP('bnt.menu.list')"
      :data="menuList"
      style="width: 100%; margin-bottom: 20px; margin-top: 10px"
      row-key="id"
      border
      :default-expand-all="true"
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="name" label="菜单名称" width="160" />
      <el-table-column label="图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="perms" label="权限标识" width="160" />
      <el-table-column prop="path" label="路由地址" width="120" />
      <el-table-column prop="component" label="组件路径" width="160" />
      <el-table-column prop="sortValue" label="排序" width="60" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status === 1" disabled="true">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column label="操作" width="180" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="success"
            v-if="scope.row.type!=2 && $hasBP('bnt.menu.insert')"
            icon="el-icon-plus"
            size="mini"
            @click="add(scope.row)"
            title="添加菜单"
          />
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            v-if="$hasBP('bnt.menu.edit')"
            @click="edit(scope.row)"
            title="修改"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            v-if="$hasBP('bnt.menu.delete')"
            @click="removeDataById(scope.row)"
            title="删除"
            :disabled="scope.row.children.length > 0"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <el-form
        :model="menu"
        label-width="150px"
        size="small"
        style="padding-right: 40px"
      >
        <el-form-item label="上级节点">
          <el-input v-model="menu.parentName" :disabled="true" />
        </el-form-item>
        <el-form-item label="菜单类型" prop="type">
          <el-radio-group v-model="menu.type">
            <el-radio :label="0" v-if="type0Disabled">目录</el-radio>
            <el-radio :label="1" v-if="type1Disabled">菜单</el-radio>
            <el-radio :label="2" v-if="type2Disabled">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="menu.name" />
        </el-form-item>
        <el-form-item label="图标" prop="icon" v-if="menu.type !== 2">
          <el-select v-model="menu.icon" clearable>
            <el-option
              v-for="item in iconList"
              :key="item.class"
              :label="item.class"
              :value="item.class"
            >
              <span style="float: left">
                <i :class="item.class"></i>
                <!-- 如果动态显示图标，这里添加判断 -->
              </span>
              <span style="padding-left: 6px">{{ item.class }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="menu.sortValue"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item prop="path" v-if="menu.type!==2">
          <span slot="label">
            <el-tooltip content="访问的路由地址，如：`user`" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            路由地址
          </span>
          <el-input v-model="menu.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item prop="component" v-if="menu.type == 1">
          <span slot="label">
            <el-tooltip
              content="访问的组件路径，如：`system/user/index`，默认在`views`目录下"
              placement="top"
            >
              <i class="el-icon-question"></i>
            </el-tooltip>
            组件路径
          </span>
          <el-input v-model="menu.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item v-if="menu.type === 2">
          <el-input
            v-model="menu.perms"
            placeholder="请输入权限标识"
            maxlength="100"
          />
          <span slot="label">
            <el-tooltip
              content="控制器中定义的权限字符，如：@PreAuthorize(hasAuthority('bnt.sysRole.list'))"
              placement="top"
            >
              <i class="el-icon-question"></i>
            </el-tooltip>
            权限字符
          </span>
        </el-form-item>
        <el-form-item label="状态" prop="type">
          <el-radio-group v-model="menu.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="dialogVisible = false"
          size="small"
          icon="el-icon-refresh-right"
          >取 消</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-check"
          @click="saveOrUpdate()"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMenuList,
  deleteMenu,
  insertMenu,
  updateMenu,
} from "@/api/menu/menu";

const defaultForm = {
  id: "",
  parentId: "",
  name: "",
  type: 0,
  path: "",
  component: "",
  perms: "",
  icon: "",
  sortValue: 1,
  status: 1,
};

export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      menuList: [],
      type0Disabled: false,
      type1Disabled: false,
      type2Disabled: false,

      menu: defaultForm,
      saveBtnDisabled: false,
      iconList: [
        {
          class: "el-icon-s-tools",
        },
        {
          class: "el-icon-s-custom",
        },
        {
          class: "el-icon-setting",
        },
        {
          class: "el-icon-user-solid",
        },
        {
          class: "el-icon-s-help",
        },
        {
          class: "el-icon-phone",
        },
        {
          class: "el-icon-s-unfold",
        },
        {
          class: "el-icon-s-operation",
        },
        {
          class: "el-icon-more-outline",
        },
        {
          class: "el-icon-s-check",
        },
        {
          class: "el-icon-tickets",
        },
        {
          class: "el-icon-s-goods",
        },
        {
          class: "el-icon-document-remove",
        },
        {
          class: "el-icon-warning",
        },
        {
          class: "el-icon-warning-outline",
        },
        {
          class: "el-icon-question",
        },
        {
          class: "el-icon-info",
        },
      ],
      dialogVisible: false,
      dialogTitle: "",
    };
  },
  computed: {},
  watch: {},
  methods: {
    add(row) {
      this.menu = Object.assign({}, defaultForm);
      this.menu.parentId = row.id;
      this.menu.parentName = row.name;
      this.dialogVisible = true;
      this.dialogTitle = "添加下级节点";
      if (row.type === 0) {
        this.menu.type = 1;
        this.type0Disabled = false;
        this.type1Disabled = true;
        this.type2Disabled = false;
      } else if (row.type === 1) {
        this.menu.type = 2;
        this.type0Disabled = false;
        this.type1Disabled = false;
        this.type2Disabled = true;
      } else {
        this.dialogTitle = "添加目录节点";
        this.menu.type = 0;
        this.menu.component = "Layout";
        this.menu.parentId = 0;
        this.type0Disabled = true;
        this.type1Disabled = false;
        this.type2Disabled = false;
      }
    },
    edit(row) {
      this.menu = row;
      this.dialogVisible = true;
      this.dialogTitle = "修改当前节点";
      if (this.menu.type === 0) {
        this.type0Disabled = true;
        this.type1Disabled = false;
        this.type2Disabled = false;
      } else if (this.menu.type === 1) {
        this.type0Disabled = false;
        this.type1Disabled = true;
        this.type2Disabled = false;
      } else {
        this.type0Disabled = true;
        this.type1Disabled = false;
        this.type2Disabled = false;
      }
    },
    save() {
      insertMenu(this.menu).then((res) => {
        if (res.data == true) {
          this.$message.success("添加成功");
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    update() {
      updateMenu(this.menu).then((res) => {
        if (res.data == true) {
          this.$message.success("修改成功");
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    saveOrUpdate() {
      if (this.menu.id == "") {
        this.save();
      } else {
        this.update();
      }
      this.dialogVisible = false;
      this.initMenuList();
      this.menu = Object.assign({}, defaultForm);
    },
    removeDataById(row) {
      deleteMenu(row).then((res) => {
        if (res) {
          this.$message.success("删除成功");
        } else {
          this.$message.error("删除失败");
        }
        this.initMenuList();
      });
    },
    initMenuList() {
      getMenuList().then((res) => {
        this.menuList = res.data;
      });
    },
  },
  created() {},
  mounted() {
    this.initMenuList();
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
};
</script>
<style scoped>
</style>